package com.example.listafacil.telas;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.listafacil.R;
import com.example.listafacil.BaseDados.BancoDeDados;
import com.example.listafacil.BaseDados.TabelasCampos;
import com.example.listafacil.componentes.ItemDeCompra;

public class TelaItensPararComprar extends Fragment {
	private BancoDeDados bd;

	public static TelaItensPararComprar newInstance() {
		TelaItensPararComprar fragment = new TelaItensPararComprar();
		return fragment;
	}

	public TelaItensPararComprar() {

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		bd = new BancoDeDados(getActivity());
		final View rootView = inflater.inflate(R.layout.tela_itens_de_compra, container, false);
		final Button btnAdicionar = (Button) rootView.findViewById(R.id.btn_adicionar_item);
		btnAdicionar.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

				builder.setMessage("Nome do item").setTitle("Adicionar item");

				final EditText edt = new EditText(getActivity());
				builder.setView(edt);

				// Add the buttons
				builder.setPositiveButton("Salvar", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						System.out.println("sim");
						Map<String, String> valores = new HashMap<String, String>(1);
						valores.put(TabelasCampos.TabelaItem.NOME, edt.getText().toString());
						adicionarLinha(rootView, valores,true);

					}
				});
				builder.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						System.out.println("nao");
					}
				});

				builder.create().show();

			}
		});

		List<Map<String, String>> listarTabelaItem = bd.listarTabelaItem();
		for (Map<String, String> item : listarTabelaItem) {
			adicionarLinha(rootView, item, false);
		}

		return rootView;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		((TelaPrincipal) activity).setTituloDaSecao("Itens");
	}

	private void adicionarLinha(View v, Map<String, String> valores, boolean deveSalvar) {
		final ItemDeCompra linha = new ItemDeCompra(getActivity(), valores, false) {

			@Override
			public void caixadeTextoPerdeuOFoco(ItemDeCompra linha) {}

			@Override
			public void checkado() {
				Map<String, String> valores = recuperarValores();
				int resultado = bd.atualizarTabelaItem(valores);
				if (resultado != -1) {
					Toast.makeText(getActivity(), "Atualizou", Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(getActivity(), "Nao atualizou", Toast.LENGTH_SHORT).show();
				}
				valores.put(TabelasCampos.TabelaLista.ID_ITEM, valores.get(TabelasCampos.TabelaItem.ID_ITEM));
				bd.inserirTabelaLista(valores);
			}

			@Override
			public void cliqueNoBotaoDeRemover() {
				removeAllViews();
				int resultado = bd.deletarTabelaItem(Integer.parseInt(recuperarValores().get(TabelasCampos.TabelaItem.ID_ITEM)));
				if (resultado != -1) {
					Toast.makeText(getActivity(), "Atualizou", Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(getActivity(), "Nao atualizou", Toast.LENGTH_SHORT).show();
				}
			}

		};
		((LinearLayout) v.findViewById(R.id.centro)).addView(linha);
		if (deveSalvar) {
			long resultado = bd.inserirTabelaItem(linha.recuperarValores());
			if (resultado != -1) {
				Toast.makeText(getActivity(), "Salvou", Toast.LENGTH_SHORT).show();
			} else {
				Toast.makeText(getActivity(), "Nao salvou", Toast.LENGTH_SHORT).show();
			}
		}

	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		bd.close();
	}

	@Override
	public void onStart() {
		super.onStart();
	}

	@Override
	public void onStop() {
		super.onStop();
	}
}
